package com.hmall.item.config;

import com.hmall.item.constants.MQConstants;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    //创建交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(MQConstants.ITEM_EXCHANGE,true,false);
    }
    //创建队列
    @Bean
    public Queue createInserQueue(){
        return QueueBuilder.durable(MQConstants.ITEM_INSERT_QUEUE).build();
    }

    //创建删除队列
    @Bean
    public Queue createDeleteQueue(){
        return QueueBuilder.durable(MQConstants.ITEM_DELETE_QUEUE).build();
    }

    //绑定交换机与指定新增或修改路由key的队友
    @Bean
    public Binding bindingInsertQueue(DirectExchange directExchange, Queue createInserQueue){
        return BindingBuilder.bind(createInserQueue).to(directExchange).with(MQConstants.ITEM_INSERT_KEY);
    }

    //绑定交换机与指定删除路由key的队友
    @Bean
    public Binding bindingDeleteQueue(DirectExchange directExchange,Queue createDeleteQueue){
        return BindingBuilder.bind(createDeleteQueue).to(directExchange).with(MQConstants.ITEM_DELETE_KEY);
    }
}
